package com.jake.spring.cloud.rabbitmq.stomp1.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
@ConfigurationProperties(prefix = "websocket.stomp")
@Data
public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {

    private String endpoint;

    private String[] destinationPrefixes;

    private String relayHost;

    private int relayPort;

    private String clientLogin;

    private String clientPasscode;

    private String[] applicationDestinationPrefixes;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint(endpoint)
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay(destinationPrefixes)
                .setRelayHost(relayHost)
                .setRelayPort(relayPort)
                .setClientLogin(clientLogin)
                .setClientPasscode(clientPasscode);
        registry.setApplicationDestinationPrefixes(applicationDestinationPrefixes);
    }
}
